{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Vector search in Python (Azure AI Search)\n",
    "\n",
    "This code demonstrates how to use Azure AI Search by using the push API to insert vectors into your search index:\n",
    "\n",
    "+ Create an index schema\n",
    "+ Load the sample data from a local folder\n",
    "+ Embed the documents in-memory using Azure OpenAI's text-embedding-3-large model\n",
    "+ Index the vector and nonvector fields on Azure AI Search\n",
    "+ Run a series of vector and hybrid queries, including metadata filtering and hybrid (text + vectors) search. \n",
    "\n",
    "The code uses Azure OpenAI to generate embeddings for title and content fields. You'll need access to Azure OpenAI to run this demo.\n",
    "\n",
    "The code reads the `text-sample.json` file, which contains the input data for which embeddings need to be generated.\n",
    "\n",
    "The output is a combination of human-readable text and embeddings that can be pushed into a search index.\n",
    "\n",
    "## Prerequisites\n",
    "\n",
    "+ An Azure subscription, with [access to Azure OpenAI](https://aka.ms/oai/access). You must have the Azure OpenAI service name and an API key.\n",
    "\n",
    "+ A deployment of the text-embedding-3-large embedding model.\n",
    "\n",
    "+ Azure AI Search, any tier, but choose a service that has sufficient capacity for your vector index. We recommend Basic or higher. [Enable semantic ranking](https://learn.microsoft.com/azure/search/semantic-how-to-enable-disable) if you want to run the hybrid query with semantic ranking.\n",
    "\n",
    "We used Python 3.11, [Visual Studio Code with the Python extension](https://code.visualstudio.com/docs/python/python-tutorial), and the [Jupyter extension](https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter) to test this example."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Set up a Python virtual environment in Visual Studio Code\n",
    "\n",
    "1. Open the Command Palette (Ctrl+Shift+P).\n",
    "1. Search for **Python: Create Environment**.\n",
    "1. Select **Venv**.\n",
    "1. Select a Python interpreter. Choose 3.10 or later.\n",
    "\n",
    "It can take a minute to set up. If you run into problems, see [Python environments in VS Code](https://code.visualstudio.com/docs/python/environments)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Install packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "! pip install -r azure-search-vector-python-sample-requirements.txt --quiet"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import required libraries and environment variables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "from dotenv import load_dotenv\n",
    "from azure.identity import DefaultAzureCredential\n",
    "from azure.core.credentials import AzureKeyCredential\n",
    "import os\n",
    "\n",
    "load_dotenv(override=True) # take environment variables from .env.\n",
    "\n",
    "# The following variables from your .env file are used in this notebook\n",
    "endpoint = os.environ[\"AZURE_SEARCH_SERVICE_ENDPOINT\"]\n",
    "credential = AzureKeyCredential(os.getenv(\"AZURE_SEARCH_ADMIN_KEY\", \"\")) if len(os.getenv(\"AZURE_SEARCH_ADMIN_KEY\", \"\")) > 0 else DefaultAzureCredential()\n",
    "index_name = os.getenv(\"AZURE_SEARCH_INDEX\", \"vectest\")\n",
    "azure_openai_endpoint = os.environ[\"AZURE_OPENAI_ENDPOINT\"]\n",
    "azure_openai_key = os.getenv(\"AZURE_OPENAI_KEY\", \"\") if len(os.getenv(\"AZURE_OPENAI_KEY\", \"\")) > 0 else None\n",
    "azure_openai_embedding_deployment = os.getenv(\"AZURE_OPENAI_EMBEDDING_DEPLOYMENT\", \"text-embedding-3-large\")\n",
    "azure_openai_embedding_dimensions = int(os.getenv(\"AZURE_OPENAI_EMBEDDING_DIMENSIONS\", 1024))\n",
    "embedding_model_name = os.getenv(\"AZURE_OPENAI_EMBEDDING_DEPLOYMENT\", \"text-embedding-3-large\")\n",
    "azure_openai_api_version = os.getenv(\"AZURE_OPENAI_API_VERSION\", \"2024-10-21\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create embeddings\n",
    "Read your data, generate OpenAI embeddings and export to a format to insert your Azure AI Search index:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "from openai import AzureOpenAI\n",
    "from azure.identity import DefaultAzureCredential, get_bearer_token_provider\n",
    "import json\n",
    "\n",
    "openai_credential = DefaultAzureCredential()\n",
    "token_provider = get_bearer_token_provider(openai_credential, \"https://cognitiveservices.azure.com/.default\")\n",
    "\n",
    "client = AzureOpenAI(\n",
    "    azure_deployment=azure_openai_embedding_deployment,\n",
    "    api_version=azure_openai_api_version,\n",
    "    azure_endpoint=azure_openai_endpoint,\n",
    "    api_key=azure_openai_key,\n",
    "    azure_ad_token_provider=token_provider if not azure_openai_key else None\n",
    ")\n",
    "\n",
    "# Generate Document Embeddings using OpenAI 3 large\n",
    "# Read the text-sample.json\n",
    "path = os.path.join('..', '..', '..', 'data', 'text-sample.json')\n",
    "with open(path, 'r', encoding='utf-8') as file:\n",
    "    input_data = json.load(file)\n",
    "\n",
    "titles = [item['title'] for item in input_data]\n",
    "content = [item['content'] for item in input_data]\n",
    "title_response = client.embeddings.create(input=titles, model=embedding_model_name, dimensions=azure_openai_embedding_dimensions)\n",
    "title_embeddings = [item.embedding for item in title_response.data]\n",
    "content_response = client.embeddings.create(input=content, model=embedding_model_name, dimensions=azure_openai_embedding_dimensions)\n",
    "content_embeddings = [item.embedding for item in content_response.data]\n",
    "\n",
    "# Generate embeddings for title and content fields\n",
    "for i, item in enumerate(input_data):\n",
    "    title = item['title']\n",
    "    content = item['content']\n",
    "    item['titleVector'] = title_embeddings[i]\n",
    "    item['contentVector'] = content_embeddings[i]\n",
    "\n",
    "# Output embeddings to docVectors.json file\n",
    "output_path = os.path.join('output', 'docVectors.json')\n",
    "output_directory = os.path.dirname(output_path)\n",
    "if not os.path.exists(output_directory):\n",
    "    os.makedirs(output_directory)\n",
    "with open(output_path, \"w\") as f:\n",
    "    json.dump(input_data, f)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create your search index\n",
    "\n",
    "Create your search index schema and vector search configuration. If you get an error, check the search service for available quota and check the .env file to make sure you're using a unique search index name."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "my-demo-index created\n"
     ]
    }
   ],
   "source": [
    "from azure.search.documents.indexes import SearchIndexClient\n",
    "from azure.search.documents.indexes.models import (\n",
    "    SimpleField,\n",
    "    SearchFieldDataType,\n",
    "    SearchableField,\n",
    "    SearchField,\n",
    "    VectorSearch,\n",
    "    HnswAlgorithmConfiguration,\n",
    "    VectorSearchProfile,\n",
    "    SemanticConfiguration,\n",
    "    SemanticPrioritizedFields,\n",
    "    SemanticField,\n",
    "    SemanticSearch,\n",
    "    SearchIndex,\n",
    "    AzureOpenAIVectorizer,\n",
    "    AzureOpenAIVectorizerParameters\n",
    ")\n",
    "\n",
    "\n",
    "# Create a search index\n",
    "index_client = SearchIndexClient(\n",
    "    endpoint=endpoint, credential=credential)\n",
    "fields = [\n",
    "    SimpleField(name=\"id\", type=SearchFieldDataType.String, key=True, sortable=True, filterable=True, facetable=True),\n",
    "    SearchableField(name=\"title\", type=SearchFieldDataType.String),\n",
    "    SearchableField(name=\"content\", type=SearchFieldDataType.String),\n",
    "    SearchableField(name=\"category\", type=SearchFieldDataType.String,\n",
    "                    filterable=True),\n",
    "    SearchField(name=\"titleVector\", type=SearchFieldDataType.Collection(SearchFieldDataType.Single),\n",
    "                searchable=True, vector_search_dimensions=azure_openai_embedding_dimensions, vector_search_profile_name=\"myHnswProfile\"),\n",
    "    SearchField(name=\"contentVector\", type=SearchFieldDataType.Collection(SearchFieldDataType.Single),\n",
    "                searchable=True, vector_search_dimensions=azure_openai_embedding_dimensions, vector_search_profile_name=\"myHnswProfile\"),\n",
    "]\n",
    "\n",
    "# Configure the vector search configuration  \n",
    "vector_search = VectorSearch(\n",
    "    algorithms=[\n",
    "        HnswAlgorithmConfiguration(\n",
    "            name=\"myHnsw\"\n",
    "        )\n",
    "    ],\n",
    "    profiles=[\n",
    "        VectorSearchProfile(\n",
    "            name=\"myHnswProfile\",\n",
    "            algorithm_configuration_name=\"myHnsw\",\n",
    "            vectorizer_name=\"myVectorizer\"\n",
    "        )\n",
    "    ],\n",
    "    vectorizers=[\n",
    "        AzureOpenAIVectorizer(\n",
    "            vectorizer_name=\"myVectorizer\",\n",
    "            parameters=AzureOpenAIVectorizerParameters(\n",
    "                resource_url=azure_openai_endpoint,\n",
    "                deployment_name=azure_openai_embedding_deployment,\n",
    "                model_name=embedding_model_name,\n",
    "                api_key=azure_openai_key\n",
    "            )\n",
    "        )\n",
    "    ]\n",
    ")\n",
    "\n",
    "\n",
    "\n",
    "semantic_config = SemanticConfiguration(\n",
    "    name=\"my-semantic-config\",\n",
    "    prioritized_fields=SemanticPrioritizedFields(\n",
    "        title_field=SemanticField(field_name=\"title\"),\n",
    "        keywords_fields=[SemanticField(field_name=\"category\")],\n",
    "        content_fields=[SemanticField(field_name=\"content\")]\n",
    "    )\n",
    ")\n",
    "\n",
    "# Create the semantic settings with the configuration\n",
    "semantic_search = SemanticSearch(configurations=[semantic_config])\n",
    "\n",
    "# Create the search index with the semantic settings\n",
    "index = SearchIndex(name=index_name, fields=fields,\n",
    "                    vector_search=vector_search, semantic_search=semantic_search)\n",
    "result = index_client.create_or_update_index(index)\n",
    "print(f'{result.name} created')\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Insert text and embeddings into vector store\n",
    "Add texts and metadata from the JSON data to the vector store:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Uploaded 108 documents\n"
     ]
    }
   ],
   "source": [
    "from azure.search.documents import SearchClient\n",
    "import json\n",
    "\n",
    "# Upload some documents to the index\n",
    "output_path = os.path.join('output', 'docVectors.json')\n",
    "output_directory = os.path.dirname(output_path)\n",
    "if not os.path.exists(output_directory):\n",
    "    os.makedirs(output_directory)\n",
    "with open(output_path, 'r') as file:  \n",
    "    documents = json.load(file)  \n",
    "search_client = SearchClient(endpoint=endpoint, index_name=index_name, credential=credential)\n",
    "result = search_client.upload_documents(documents)\n",
    "print(f\"Uploaded {len(documents)} documents\") "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you are indexing a very large number of documents, you can use the `SearchIndexingBufferedSender` which is an optimized way to automatically index the docs as it will handle the batching for you:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Uploaded 108 documents in total\n"
     ]
    }
   ],
   "source": [
    "from azure.search.documents import SearchIndexingBufferedSender\n",
    "\n",
    "# Upload some documents to the index  \n",
    "with open(output_path, 'r') as file:  \n",
    "    documents = json.load(file)  \n",
    "  \n",
    "# Use SearchIndexingBufferedSender to upload the documents in batches optimized for indexing  \n",
    "with SearchIndexingBufferedSender(  \n",
    "    endpoint=endpoint,  \n",
    "    index_name=index_name,  \n",
    "    credential=credential,  \n",
    ") as batch_client:  \n",
    "    # Add upload actions for all documents  \n",
    "    batch_client.upload_documents(documents=documents)  \n",
    "print(f\"Uploaded {len(documents)} documents in total\")  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Helper code to print results\n",
    "\n",
    "from azure.search.documents import SearchItemPaged\n",
    "\n",
    "def print_results(results: SearchItemPaged[dict]):\n",
    "    semantic_answers = results.get_answers()\n",
    "    if semantic_answers:\n",
    "        for answer in semantic_answers:\n",
    "            if answer.highlights:\n",
    "                print(f\"Semantic Answer: {answer.highlights}\")\n",
    "            else:\n",
    "                print(f\"Semantic Answer: {answer.text}\")\n",
    "            print(f\"Semantic Answer Score: {answer.score}\\n\")\n",
    "\n",
    "    for result in results:\n",
    "        print(f\"Title: {result['title']}\")  \n",
    "        print(f\"Score: {result['@search.score']}\")\n",
    "        if result.get('@search.reranker_score'):\n",
    "            print(f\"Reranker Score: {result['@search.reranker_score']}\")\n",
    "        print(f\"Content: {result['content']}\")  \n",
    "        print(f\"Category: {result['category']}\\n\")\n",
    "\n",
    "        captions = result[\"@search.captions\"]\n",
    "        if captions:\n",
    "            caption = captions[0]\n",
    "            if caption.highlights:\n",
    "                print(f\"Caption: {caption.highlights}\\n\")\n",
    "            else:\n",
    "                print(f\"Caption: {caption.text}\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Perform a vector similarity search\n",
    "\n",
    "This example shows a pure vector search using a pre-computed embedding"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Title: Azure DevOps\n",
      "Score: 0.6064064\n",
      "Content: Azure DevOps is a suite of services that help you plan, build, and deploy applications. It includes Azure Boards for work item tracking, Azure Repos for source code management, Azure Pipelines for continuous integration and continuous deployment, Azure Test Plans for manual and automated testing, and Azure Artifacts for package management. DevOps supports a wide range of programming languages, frameworks, and platforms, making it easy to integrate with your existing development tools and processes. It also integrates with other Azure services, such as Azure App Service and Azure Functions.\n",
      "Category: Developer Tools\n",
      "\n",
      "Title: Azure DevTest Labs\n",
      "Score: 0.5913068\n",
      "Content: Azure DevTest Labs is a fully managed service that enables you to create, manage, and share development and test environments in Azure. It provides features like custom templates, cost management, and integration with Azure DevOps. DevTest Labs supports various platforms, such as Windows, Linux, and Kubernetes. You can use Azure DevTest Labs to improve your application development lifecycle, reduce your costs, and ensure the consistency of your environments. It also integrates with other Azure services, such as Azure Virtual Machines and Azure App Service.\n",
      "Category: Developer Tools\n",
      "\n",
      "Title: Azure Application Insights\n",
      "Score: 0.56719214\n",
      "Content: Azure Application Insights is an application performance management service that enables you to monitor, diagnose, and troubleshoot your applications and infrastructure. It provides features like real-time telemetry, dependency mapping, and live metrics. Application Insights supports various platforms, such as .NET, Java, Node.js, and Python. You can use Azure Application Insights to detect and diagnose issues, optimize your performance, and ensure the availability of your applications. It also integrates with other Azure services, such as Azure Monitor and Azure Log Analytics.\n",
      "Category: Management + Governance\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from azure.search.documents.models import VectorizedQuery\n",
    "\n",
    "# Pure Vector Search\n",
    "query = \"tools for software development\"  \n",
    "embedding = client.embeddings.create(input=query, model=embedding_model_name, dimensions=azure_openai_embedding_dimensions).data[0].embedding\n",
    "\n",
    "# 50 is an optimal value for k_nearest_neighbors when performing vector search\n",
    "# To learn more about how vector ranking works, please visit https://learn.microsoft.com/azure/search/vector-search-ranking\n",
    "vector_query = VectorizedQuery(vector=embedding, k_nearest_neighbors=50, fields=\"contentVector\")\n",
    "  \n",
    "results = search_client.search(  \n",
    "    search_text=None,  \n",
    "    vector_queries= [vector_query],\n",
    "    select=[\"title\", \"content\", \"category\"],\n",
    "    top=3\n",
    ")  \n",
    "  \n",
    "print_results(results)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Perform a vector similarity search using a vectorizable text query"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This example shows a pure vector search using the vectorizable text query, all you need to do is pass in text and your vectorizer will handle the query vectorization."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Title: Azure DevOps\n",
      "Score: 0.6064064\n",
      "Content: Azure DevOps is a suite of services that help you plan, build, and deploy applications. It includes Azure Boards for work item tracking, Azure Repos for source code management, Azure Pipelines for continuous integration and continuous deployment, Azure Test Plans for manual and automated testing, and Azure Artifacts for package management. DevOps supports a wide range of programming languages, frameworks, and platforms, making it easy to integrate with your existing development tools and processes. It also integrates with other Azure services, such as Azure App Service and Azure Functions.\n",
      "Category: Developer Tools\n",
      "\n",
      "Title: Azure DevTest Labs\n",
      "Score: 0.5913068\n",
      "Content: Azure DevTest Labs is a fully managed service that enables you to create, manage, and share development and test environments in Azure. It provides features like custom templates, cost management, and integration with Azure DevOps. DevTest Labs supports various platforms, such as Windows, Linux, and Kubernetes. You can use Azure DevTest Labs to improve your application development lifecycle, reduce your costs, and ensure the consistency of your environments. It also integrates with other Azure services, such as Azure Virtual Machines and Azure App Service.\n",
      "Category: Developer Tools\n",
      "\n",
      "Title: Azure Application Insights\n",
      "Score: 0.56719214\n",
      "Content: Azure Application Insights is an application performance management service that enables you to monitor, diagnose, and troubleshoot your applications and infrastructure. It provides features like real-time telemetry, dependency mapping, and live metrics. Application Insights supports various platforms, such as .NET, Java, Node.js, and Python. You can use Azure Application Insights to detect and diagnose issues, optimize your performance, and ensure the availability of your applications. It also integrates with other Azure services, such as Azure Monitor and Azure Log Analytics.\n",
      "Category: Management + Governance\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from azure.search.documents.models import VectorizableTextQuery\n",
    "\n",
    "# Pure Vector Search\n",
    "query = \"tools for software development\"  \n",
    "  \n",
    "vector_query = VectorizableTextQuery(text=query, k_nearest_neighbors=50, fields=\"contentVector\")\n",
    "  \n",
    "results = search_client.search(  \n",
    "    search_text=None,  \n",
    "    vector_queries= [vector_query],\n",
    "    select=[\"title\", \"content\", \"category\"],\n",
    "    top=3\n",
    ")  \n",
    "  \n",
    "print_results(results)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This example shows a pure vector search that demonstrates Azure OpenAI's text-embedding-3-large multilingual capabilities. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Title: Azure DevOps\n",
      "Score: 0.5766108\n",
      "Content: Azure DevOps is a suite of services that help you plan, build, and deploy applications. It includes Azure Boards for work item tracking, Azure Repos for source code management, Azure Pipelines for continuous integration and continuous deployment, Azure Test Plans for manual and automated testing, and Azure Artifacts for package management. DevOps supports a wide range of programming languages, frameworks, and platforms, making it easy to integrate with your existing development tools and processes. It also integrates with other Azure services, such as Azure App Service and Azure Functions.\n",
      "Category: Developer Tools\n",
      "\n",
      "Title: Azure DevTest Labs\n",
      "Score: 0.5673431\n",
      "Content: Azure DevTest Labs is a fully managed service that enables you to create, manage, and share development and test environments in Azure. It provides features like custom templates, cost management, and integration with Azure DevOps. DevTest Labs supports various platforms, such as Windows, Linux, and Kubernetes. You can use Azure DevTest Labs to improve your application development lifecycle, reduce your costs, and ensure the consistency of your environments. It also integrates with other Azure services, such as Azure Virtual Machines and Azure App Service.\n",
      "Category: Developer Tools\n",
      "\n",
      "Title: Azure Application Insights\n",
      "Score: 0.54438746\n",
      "Content: Azure Application Insights is an application performance management service that enables you to monitor, diagnose, and troubleshoot your applications and infrastructure. It provides features like real-time telemetry, dependency mapping, and live metrics. Application Insights supports various platforms, such as .NET, Java, Node.js, and Python. You can use Azure Application Insights to detect and diagnose issues, optimize your performance, and ensure the availability of your applications. It also integrates with other Azure services, such as Azure Monitor and Azure Log Analytics.\n",
      "Category: Management + Governance\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Pure Vector Search multi-lingual (e.g 'tools for software development' in Dutch)  \n",
    "query = \"tools voor softwareontwikkeling\"  \n",
    "  \n",
    "vector_query = VectorizableTextQuery(text=query, k_nearest_neighbors=50, fields=\"contentVector\")\n",
    "\n",
    "results = search_client.search(  \n",
    "    search_text=None,  \n",
    "    vector_queries= [vector_query],\n",
    "    select=[\"title\", \"content\", \"category\"],\n",
    "    top=3\n",
    ")  \n",
    "  \n",
    "print_results(results)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Perform an Exhaustive KNN exact nearest neighbor search"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This example shows how you can exhaustively search your vector index regardless of what index you have, HNSW or ExhaustiveKNN. You can use this to calculate the ground-truth values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Title: Azure DevOps\n",
      "Score: 0.6064064\n",
      "Content: Azure DevOps is a suite of services that help you plan, build, and deploy applications. It includes Azure Boards for work item tracking, Azure Repos for source code management, Azure Pipelines for continuous integration and continuous deployment, Azure Test Plans for manual and automated testing, and Azure Artifacts for package management. DevOps supports a wide range of programming languages, frameworks, and platforms, making it easy to integrate with your existing development tools and processes. It also integrates with other Azure services, such as Azure App Service and Azure Functions.\n",
      "Category: Developer Tools\n",
      "\n",
      "Title: Azure DevTest Labs\n",
      "Score: 0.5913068\n",
      "Content: Azure DevTest Labs is a fully managed service that enables you to create, manage, and share development and test environments in Azure. It provides features like custom templates, cost management, and integration with Azure DevOps. DevTest Labs supports various platforms, such as Windows, Linux, and Kubernetes. You can use Azure DevTest Labs to improve your application development lifecycle, reduce your costs, and ensure the consistency of your environments. It also integrates with other Azure services, such as Azure Virtual Machines and Azure App Service.\n",
      "Category: Developer Tools\n",
      "\n",
      "Title: Azure Application Insights\n",
      "Score: 0.56719214\n",
      "Content: Azure Application Insights is an application performance management service that enables you to monitor, diagnose, and troubleshoot your applications and infrastructure. It provides features like real-time telemetry, dependency mapping, and live metrics. Application Insights supports various platforms, such as .NET, Java, Node.js, and Python. You can use Azure Application Insights to detect and diagnose issues, optimize your performance, and ensure the availability of your applications. It also integrates with other Azure services, such as Azure Monitor and Azure Log Analytics.\n",
      "Category: Management + Governance\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Pure Vector Search\n",
    "query = \"tools for software development\"  \n",
    "  \n",
    "vector_query = VectorizableTextQuery(text=query, k_nearest_neighbors=50, fields=\"contentVector\", exhaustive=True)\n",
    "  \n",
    "results = search_client.search(  \n",
    "    search_text=None,  \n",
    "    vector_queries= [vector_query],\n",
    "    select=[\"title\", \"content\", \"category\"],\n",
    "    top=3\n",
    ")  \n",
    "  \n",
    "print_results(results)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Perform a Cross-Field Vector Search\n",
    "\n",
    "This example shows a cross-field vector search that allows you to query multiple vector fields at the same time. Note, ensure that the same embedding model was used for the vector fields you decide to query."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Title: Azure DevOps\n",
      "Score: 0.03333333507180214\n",
      "Content: Azure DevOps is a suite of services that help you plan, build, and deploy applications. It includes Azure Boards for work item tracking, Azure Repos for source code management, Azure Pipelines for continuous integration and continuous deployment, Azure Test Plans for manual and automated testing, and Azure Artifacts for package management. DevOps supports a wide range of programming languages, frameworks, and platforms, making it easy to integrate with your existing development tools and processes. It also integrates with other Azure services, such as Azure App Service and Azure Functions.\n",
      "Category: Developer Tools\n",
      "\n",
      "Title: Azure DevTest Labs\n",
      "Score: 0.032786883413791656\n",
      "Content: Azure DevTest Labs is a fully managed service that enables you to create, manage, and share development and test environments in Azure. It provides features like custom templates, cost management, and integration with Azure DevOps. DevTest Labs supports various platforms, such as Windows, Linux, and Kubernetes. You can use Azure DevTest Labs to improve your application development lifecycle, reduce your costs, and ensure the consistency of your environments. It also integrates with other Azure services, such as Azure Virtual Machines and Azure App Service.\n",
      "Category: Developer Tools\n",
      "\n",
      "Title: Azure Application Insights\n",
      "Score: 0.0317540317773819\n",
      "Content: Azure Application Insights is an application performance management service that enables you to monitor, diagnose, and troubleshoot your applications and infrastructure. It provides features like real-time telemetry, dependency mapping, and live metrics. Application Insights supports various platforms, such as .NET, Java, Node.js, and Python. You can use Azure Application Insights to detect and diagnose issues, optimize your performance, and ensure the availability of your applications. It also integrates with other Azure services, such as Azure Monitor and Azure Log Analytics.\n",
      "Category: Management + Governance\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Pure Vector Search\n",
    "query = \"tools for software development\"  \n",
    "  \n",
    "vector_query = VectorizableTextQuery(text=query, k_nearest_neighbors=50, fields=\"contentVector, titleVector\")\n",
    "\n",
    "results = search_client.search(  \n",
    "    search_text=None,  \n",
    "    vector_queries= [vector_query],\n",
    "    select=[\"title\", \"content\", \"category\"],\n",
    "    top=3\n",
    ")  \n",
    "  \n",
    "print_results(results)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Perform a Multi-Vector Search\n",
    "\n",
    "This example shows a cross-field vector search that allows you to query multiple vector fields at the same time by passing in multiple query vectors. Note, in this case, you can pass in query vectors from two different embedding models to the corresponding vector fields in your index."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Title: Azure DevOps\n",
      "Score: 0.03333333507180214\n",
      "Content: Azure DevOps is a suite of services that help you plan, build, and deploy applications. It includes Azure Boards for work item tracking, Azure Repos for source code management, Azure Pipelines for continuous integration and continuous deployment, Azure Test Plans for manual and automated testing, and Azure Artifacts for package management. DevOps supports a wide range of programming languages, frameworks, and platforms, making it easy to integrate with your existing development tools and processes. It also integrates with other Azure services, such as Azure App Service and Azure Functions.\n",
      "Category: Developer Tools\n",
      "\n",
      "Title: Azure DevTest Labs\n",
      "Score: 0.032786883413791656\n",
      "Content: Azure DevTest Labs is a fully managed service that enables you to create, manage, and share development and test environments in Azure. It provides features like custom templates, cost management, and integration with Azure DevOps. DevTest Labs supports various platforms, such as Windows, Linux, and Kubernetes. You can use Azure DevTest Labs to improve your application development lifecycle, reduce your costs, and ensure the consistency of your environments. It also integrates with other Azure services, such as Azure Virtual Machines and Azure App Service.\n",
      "Category: Developer Tools\n",
      "\n",
      "Title: Azure Application Insights\n",
      "Score: 0.0317540317773819\n",
      "Content: Azure Application Insights is an application performance management service that enables you to monitor, diagnose, and troubleshoot your applications and infrastructure. It provides features like real-time telemetry, dependency mapping, and live metrics. Application Insights supports various platforms, such as .NET, Java, Node.js, and Python. You can use Azure Application Insights to detect and diagnose issues, optimize your performance, and ensure the availability of your applications. It also integrates with other Azure services, such as Azure Monitor and Azure Log Analytics.\n",
      "Category: Management + Governance\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Multi-Vector Search\n",
    "query = \"tools for software development\"  \n",
    "  \n",
    "\n",
    "vector_query_1 = VectorizableTextQuery(text=query, k_nearest_neighbors=50, fields=\"titleVector\")\n",
    "vector_query_2 = VectorizableTextQuery(text=query, k_nearest_neighbors=50, fields=\"contentVector\")\n",
    "\n",
    "results = search_client.search(  \n",
    "    search_text=None,  \n",
    "    vector_queries=[vector_query_1, vector_query_2],\n",
    "    select=[\"title\", \"content\", \"category\"],\n",
    "    top=3\n",
    ")  \n",
    "  \n",
    "print_results(results)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Perform a weighted multi-vector search\n",
    "\n",
    "This example shows a cross-field vector search that allows you to query multiple vector fields at the same time by passing in multiple query vectors with different weighting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Title: Azure DevOps\n",
      "Score: 0.0416666679084301\n",
      "Content: Azure DevOps is a suite of services that help you plan, build, and deploy applications. It includes Azure Boards for work item tracking, Azure Repos for source code management, Azure Pipelines for continuous integration and continuous deployment, Azure Test Plans for manual and automated testing, and Azure Artifacts for package management. DevOps supports a wide range of programming languages, frameworks, and platforms, making it easy to integrate with your existing development tools and processes. It also integrates with other Azure services, such as Azure App Service and Azure Functions.\n",
      "Category: Developer Tools\n",
      "\n",
      "Title: Azure DevTest Labs\n",
      "Score: 0.04098360240459442\n",
      "Content: Azure DevTest Labs is a fully managed service that enables you to create, manage, and share development and test environments in Azure. It provides features like custom templates, cost management, and integration with Azure DevOps. DevTest Labs supports various platforms, such as Windows, Linux, and Kubernetes. You can use Azure DevTest Labs to improve your application development lifecycle, reduce your costs, and ensure the consistency of your environments. It also integrates with other Azure services, such as Azure Virtual Machines and Azure App Service.\n",
      "Category: Developer Tools\n",
      "\n",
      "Title: Azure Service Fabric\n",
      "Score: 0.0395585335791111\n",
      "Content: Azure Service Fabric is a distributed systems platform that enables you to build, deploy, and manage scalable and reliable microservices and container-based applications. It provides features like stateful services, fault tolerance, and rolling upgrades. Service Fabric supports various programming languages, such as C#, Java, and Node.js. You can use Azure Service Fabric to build cloud-native applications, modernize your existing applications, and ensure high availability and scalability. It also integrates with other Azure services, such as Azure Kubernetes Service and Azure DevOps.\n",
      "Category: Compute\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Multi-Vector Search\n",
    "query = \"tools for software development\"  \n",
    "  \n",
    "\n",
    "vector_query_1 = VectorizableTextQuery(text=query, k_nearest_neighbors=50, fields=\"titleVector\", weight=2)\n",
    "vector_query_2 = VectorizableTextQuery(text=query, k_nearest_neighbors=50, fields=\"contentVector\", weight=0.5)\n",
    "\n",
    "results = search_client.search(  \n",
    "    search_text=None,  \n",
    "    vector_queries=[vector_query_1, vector_query_2],\n",
    "    select=[\"title\", \"content\", \"category\"],\n",
    "    top=3\n",
    ")  \n",
    "  \n",
    "print_results(results)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Perform a Pure Vector Search with a filter\n",
    "This example shows how to apply filters on your index. Note, that you can choose whether you want to use Pre-Filtering (default) or Post-Filtering."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Title: Azure DevOps\n",
      "Score: 0.6064064\n",
      "Content: Azure DevOps is a suite of services that help you plan, build, and deploy applications. It includes Azure Boards for work item tracking, Azure Repos for source code management, Azure Pipelines for continuous integration and continuous deployment, Azure Test Plans for manual and automated testing, and Azure Artifacts for package management. DevOps supports a wide range of programming languages, frameworks, and platforms, making it easy to integrate with your existing development tools and processes. It also integrates with other Azure services, such as Azure App Service and Azure Functions.\n",
      "Category: Developer Tools\n",
      "\n",
      "Title: Azure DevTest Labs\n",
      "Score: 0.5913068\n",
      "Content: Azure DevTest Labs is a fully managed service that enables you to create, manage, and share development and test environments in Azure. It provides features like custom templates, cost management, and integration with Azure DevOps. DevTest Labs supports various platforms, such as Windows, Linux, and Kubernetes. You can use Azure DevTest Labs to improve your application development lifecycle, reduce your costs, and ensure the consistency of your environments. It also integrates with other Azure services, such as Azure Virtual Machines and Azure App Service.\n",
      "Category: Developer Tools\n",
      "\n",
      "Title: Azure App Configuration\n",
      "Score: 0.55565226\n",
      "Content: Azure App Configuration is a fully managed configuration service that enables you to centrally manage and distribute your application settings and feature flags. It provides features like key-value storage, versioning, and access control. App Configuration supports various platforms, such as .NET, Java, Node.js, and Python. You can use Azure App Configuration to build and deploy your applications, ensure the consistency of your settings, and improve your application lifecycle. It also integrates with other Azure services, such as Azure App Service and Azure Functions.\n",
      "Category: Developer Tools\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from azure.search.documents.models import VectorFilterMode\n",
    "\n",
    "# Pure Vector Search\n",
    "query = \"tools for software development\"  \n",
    "  \n",
    "vector_query = VectorizableTextQuery(text=query, k_nearest_neighbors=50, fields=\"contentVector\")\n",
    "\n",
    "results = search_client.search(  \n",
    "    search_text=None,  \n",
    "    vector_queries= [vector_query],\n",
    "    vector_filter_mode=VectorFilterMode.PRE_FILTER,\n",
    "    filter=\"category eq 'Developer Tools'\",\n",
    "    select=[\"title\", \"content\", \"category\"],\n",
    "    top=3\n",
    ")\n",
    "  \n",
    "print_results(results)\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Perform a Hybrid Search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Title: Azure Storage\n",
      "Score: 0.03333333507180214\n",
      "Content: Azure Storage is a scalable, durable, and highly available cloud storage service that supports a variety of data types, including blobs, files, queues, and tables. It provides a massively scalable object store for unstructured data. Storage supports data redundancy and geo-replication, ensuring high durability and availability. It offers a variety of data access and management options, including REST APIs, SDKs, and Azure Portal. You can secure your data using encryption at rest and in transit.\n",
      "Category: Storage\n",
      "\n",
      "Title: Azure Data Lake Storage\n",
      "Score: 0.03226646035909653\n",
      "Content: Azure Data Lake Storage is a scalable, secure, and cost-effective storage service for big data analytics. It provides features like unlimited storage, hierarchical namespace, and fine-grained access control. Data Lake Storage supports various data types, such as structured, semi-structured, and unstructured data. You can use Data Lake Storage to store and analyze your data, build data lakes, and integrate with other Azure services. It also integrates with other Azure services, such as Azure Databricks and Azure Machine Learning.\n",
      "Category: Storage\n",
      "\n",
      "Title: Azure File Storage\n",
      "Score: 0.03226646035909653\n",
      "Content: Azure File Storage is a fully managed, scalable, and secure file sharing service that enables you to store and access your files over the Server Message Block (SMB) protocol. It provides features like snapshots, shared access signatures, and integration with Azure Backup. File Storage supports various platforms, such as Windows, Linux, and macOS. You can use Azure File Storage to build file sharing solutions, lift and shift your applications to the cloud, and simplify your data management. It also integrates with other Azure services, such as Azure Virtual Machines and Azure Kubernetes Service.\n",
      "Category: Storage\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Hybrid Search\n",
    "query = \"scalable storage solution\"  \n",
    "  \n",
    "vector_query = VectorizableTextQuery(text=query, k_nearest_neighbors=50, fields=\"contentVector\")\n",
    "\n",
    "results = search_client.search(  \n",
    "    search_text=query,  \n",
    "    vector_queries=[vector_query],\n",
    "    select=[\"title\", \"content\", \"category\"],\n",
    "    top=3\n",
    ")  \n",
    "  \n",
    "print_results(results)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Perform a weighted hybrid search\n",
    "\n",
    "Change the weighting of the vector query compared to the text query"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Title: Azure Storage\n",
      "Score: 0.020000001415610313\n",
      "Content: Azure Storage is a scalable, durable, and highly available cloud storage service that supports a variety of data types, including blobs, files, queues, and tables. It provides a massively scalable object store for unstructured data. Storage supports data redundancy and geo-replication, ensuring high durability and availability. It offers a variety of data access and management options, including REST APIs, SDKs, and Azure Portal. You can secure your data using encryption at rest and in transit.\n",
      "Category: Storage\n",
      "\n",
      "Title: Azure Data Lake Storage\n",
      "Score: 0.01956804469227791\n",
      "Content: Azure Data Lake Storage is a scalable, secure, and cost-effective storage service for big data analytics. It provides features like unlimited storage, hierarchical namespace, and fine-grained access control. Data Lake Storage supports various data types, such as structured, semi-structured, and unstructured data. You can use Data Lake Storage to store and analyze your data, build data lakes, and integrate with other Azure services. It also integrates with other Azure services, such as Azure Databricks and Azure Machine Learning.\n",
      "Category: Storage\n",
      "\n",
      "Title: Azure Table Storage\n",
      "Score: 0.019254032522439957\n",
      "Content: Azure Table Storage is a fully managed, NoSQL datastore that enables you to store and query large amounts of structured, non-relational data. It provides features like automatic scaling, schema-less design, and a RESTful API. Table Storage supports various data types, such as strings, numbers, and booleans. You can use Azure Table Storage to store and manage your data, build scalable applications, and reduce the cost of your storage. It also integrates with other Azure services, such as Azure Functions and Azure Cosmos DB.\n",
      "Category: Storage\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Hybrid Search\n",
    "query = \"scalable storage solution\"  \n",
    "  \n",
    "vector_query = VectorizableTextQuery(text=query, k_nearest_neighbors=50, fields=\"contentVector\", weight=0.2)\n",
    "\n",
    "results = search_client.search(  \n",
    "    search_text=query,  \n",
    "    vector_queries=[vector_query],\n",
    "    select=[\"title\", \"content\", \"category\"],\n",
    "    top=3\n",
    ")  \n",
    "  \n",
    "print_results(results)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Perform a Semantic Hybrid Search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Semantic Answer: Azure Data Share is a<em> fully managed data sharing service </em>that enables you to securely share and receive data from other<em> organizations </em>using Azure. It provides features like data snapshotting, change tracking, and access control. Data Share supports various data sources, such as Azure Blob Storage, Azure Data Lake Storage, and Azure SQL Database..\n",
      "Semantic Answer Score: 0.9100000262260437\n",
      "\n",
      "Title: Azure Data Share\n",
      "Score: 0.01895187795162201\n",
      "Reranker Score: 2.7039756774902344\n",
      "Content: Azure Data Share is a fully managed data sharing service that enables you to securely share and receive data from other organizations using Azure. It provides features like data snapshotting, change tracking, and access control. Data Share supports various data sources, such as Azure Blob Storage, Azure Data Lake Storage, and Azure SQL Database. You can use Azure Data Share to collaborate with your partners, improve your data governance, and ensure the security of your data. It also integrates with other Azure services, such as Azure Synapse Analytics and Azure Data Factory.\n",
      "Category: Analytics\n",
      "\n",
      "Caption: Azure Data Share is a<em> fully managed data sharing service </em>that enables you to securely share and receive data from other<em> organizations </em>using Azure. It provides features like data snapshotting, change tracking, and access control. Data Share supports various data sources, such as Azure Blob Storage, Azure Data Lake Storage, and Azure SQL Database..\n",
      "\n",
      "Title: Azure Cognitive Search\n",
      "Score: 0.02460317686200142\n",
      "Reranker Score: 2.349949598312378\n",
      "Content: Azure Cognitive Search is a fully managed search-as-a-service that enables you to build rich search experiences for your applications. It provides features like full-text search, faceted navigation, and filters. Azure Cognitive Search supports various data sources, such as Azure SQL Database, Azure Blob Storage, and Azure Cosmos DB. You can use Azure Cognitive Search to index your data, create custom scoring profiles, and integrate with other Azure services. It also integrates with other Azure services, such as Azure Cognitive Services and Azure Machine Learning.\n",
      "Category: AI + Machine Learning\n",
      "\n",
      "Caption: Azure Cognitive Search is<em> a fully managed search-as-a-service </em>that enables you to build rich<em> search </em>experiences for your applications. It provides features like full-text search, faceted navigation, and filters. Azure Cognitive Search supports various data sources, such as Azure SQL Database, Azure Blob Storage, and Azure Cosmos DB. You can use.\n",
      "\n",
      "Title: Azure File Storage\n",
      "Score: 0.024089636281132698\n",
      "Reranker Score: 2.164484739303589\n",
      "Content: Azure File Storage is a fully managed, scalable, and secure file sharing service that enables you to store and access your files over the Server Message Block (SMB) protocol. It provides features like snapshots, shared access signatures, and integration with Azure Backup. File Storage supports various platforms, such as Windows, Linux, and macOS. You can use Azure File Storage to build file sharing solutions, lift and shift your applications to the cloud, and simplify your data management. It also integrates with other Azure services, such as Azure Virtual Machines and Azure Kubernetes Service.\n",
      "Category: Storage\n",
      "\n",
      "Caption: Azure File Storage is a<em> fully managed, scalable, and secure file sharing service </em>that enables you to store and access your files over the Server Message Block (SMB) protocol. It provides features like snapshots, shared access signatures, and integration with Azure Backup. File Storage supports various platforms, such as Windows, Linux, and macOS..\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from azure.search.documents.models import QueryType, QueryCaptionType, QueryAnswerType\n",
    "\n",
    "# Semantic Hybrid Search\n",
    "query = \"what is azure sarch?\"\n",
    "\n",
    "vector_query = VectorizableTextQuery(text=query, k_nearest_neighbors=50, fields=\"contentVector\", exhaustive=True)\n",
    "\n",
    "results = search_client.search(  \n",
    "    search_text=query,  \n",
    "    vector_queries=[vector_query],\n",
    "    select=[\"title\", \"content\", \"category\"],\n",
    "    query_type=QueryType.SEMANTIC,\n",
    "    semantic_configuration_name='my-semantic-config',\n",
    "    query_caption=QueryCaptionType.EXTRACTIVE,\n",
    "    query_answer=QueryAnswerType.EXTRACTIVE,\n",
    "    top=3\n",
    ")\n",
    "\n",
    "print_results(results)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Perform a Semantic Hybrid Search with Query Rewriting\n",
    "\n",
    "+ Uses [query rewriting](https://learn.microsoft.com/azure/search/semantic-how-to-query-rewrite) combined with semantic ranker\n",
    "+ Set the debug parameter to queryRewrites to see what the query was rewritten to\n",
    "+ Avoid using the debug parameter in production"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Semantic Answer: Azure Data Share is a<em> fully managed data sharing service </em>that enables you to securely share and receive data from other<em> organizations </em>using Azure. It provides features like data snapshotting, change tracking, and access control. Data Share supports various data sources, such as Azure Blob Storage, Azure Data Lake Storage, and Azure SQL Database..\n",
      "Semantic Answer Score: 0.9100000262260437\n",
      "\n",
      "Title: Azure Data Share\n",
      "Score: 0.017307978123426437\n",
      "Reranker Score: 2.7039756774902344\n",
      "Content: Azure Data Share is a fully managed data sharing service that enables you to securely share and receive data from other organizations using Azure. It provides features like data snapshotting, change tracking, and access control. Data Share supports various data sources, such as Azure Blob Storage, Azure Data Lake Storage, and Azure SQL Database. You can use Azure Data Share to collaborate with your partners, improve your data governance, and ensure the security of your data. It also integrates with other Azure services, such as Azure Synapse Analytics and Azure Data Factory.\n",
      "Category: Analytics\n",
      "\n",
      "Caption: Azure Data Share is a<em> fully managed data sharing service </em>that enables you to securely share and receive data from other<em> organizations </em>using Azure. It provides features like data snapshotting, change tracking, and access control. Data Share supports various data sources, such as Azure Blob Storage, Azure Data Lake Storage, and Azure SQL Database..\n",
      "\n",
      "Title: Azure Cognitive Search\n",
      "Score: 0.03333333507180214\n",
      "Reranker Score: 2.349949598312378\n",
      "Content: Azure Cognitive Search is a fully managed search-as-a-service that enables you to build rich search experiences for your applications. It provides features like full-text search, faceted navigation, and filters. Azure Cognitive Search supports various data sources, such as Azure SQL Database, Azure Blob Storage, and Azure Cosmos DB. You can use Azure Cognitive Search to index your data, create custom scoring profiles, and integrate with other Azure services. It also integrates with other Azure services, such as Azure Cognitive Services and Azure Machine Learning.\n",
      "Category: AI + Machine Learning\n",
      "\n",
      "Caption: Azure Cognitive Search is<em> a fully managed search-as-a-service </em>that enables you to build rich<em> search </em>experiences for your applications. It provides features like full-text search, faceted navigation, and filters. Azure Cognitive Search supports various data sources, such as Azure SQL Database, Azure Blob Storage, and Azure Cosmos DB. You can use.\n",
      "\n",
      "Title: Azure File Storage\n",
      "Score: 0.02127891778945923\n",
      "Reranker Score: 2.164484739303589\n",
      "Content: Azure File Storage is a fully managed, scalable, and secure file sharing service that enables you to store and access your files over the Server Message Block (SMB) protocol. It provides features like snapshots, shared access signatures, and integration with Azure Backup. File Storage supports various platforms, such as Windows, Linux, and macOS. You can use Azure File Storage to build file sharing solutions, lift and shift your applications to the cloud, and simplify your data management. It also integrates with other Azure services, such as Azure Virtual Machines and Azure Kubernetes Service.\n",
      "Category: Storage\n",
      "\n",
      "Caption: Azure File Storage is a<em> fully managed, scalable, and secure file sharing service </em>that enables you to store and access your files over the Server Message Block (SMB) protocol. It provides features like snapshots, shared access signatures, and integration with Azure Backup. File Storage supports various platforms, such as Windows, Linux, and macOS..\n",
      "\n",
      "Text Query Rewrites:\n",
      "['what does Azure Search do', 'definition of Azure Search', 'what does AzureSearch do', 'define Azure Search', 'what does Azure Search offer']\n",
      "Vector Query Rewrites:\n",
      "['what does Azure Search do', 'definition of Azure Search', 'what does AzureSearch do', 'define Azure Search', 'what does Azure Search offer']\n"
     ]
    }
   ],
   "source": [
    "from azure.search.documents.models import QueryType, QueryCaptionType, QueryAnswerType, QueryDebugMode\n",
    "from typing import Optional\n",
    "\n",
    "# Workaround required to use debug query rewrites with the preview SDK\n",
    "import azure.search.documents._generated.models\n",
    "azure.search.documents._generated.models.SearchDocumentsResult._attribute_map[\"debug_info\"][\"key\"] = \"@search\\\\.debug\"\n",
    "from azure.search.documents._generated.models import DebugInfo\n",
    "import azure.search.documents._paging\n",
    "def get_debug_info(self) -> Optional[DebugInfo]:\n",
    "    self.continuation_token = None\n",
    "    return self._response.debug_info\n",
    "azure.search.documents._paging.SearchPageIterator.get_debug_info = azure.search.documents._paging._ensure_response(get_debug_info)\n",
    "azure.search.documents._paging.SearchItemPaged.get_debug_info = lambda self: self._first_iterator_instance().get_debug_info()\n",
    "\n",
    "# Semantic Hybrid Search with Query Rewriting\n",
    "query = \"what is azure sarch?\"\n",
    "\n",
    "vector_query = VectorizableTextQuery(text=query, k_nearest_neighbors=50, fields=\"contentVector\", query_rewrites=\"generative|count-5\")\n",
    "\n",
    "results = search_client.search(  \n",
    "    search_text=query,  \n",
    "    vector_queries=[vector_query],\n",
    "    select=[\"title\", \"content\", \"category\"],\n",
    "    query_type=QueryType.SEMANTIC,\n",
    "    semantic_configuration_name='my-semantic-config',\n",
    "    query_rewrites=\"generative|count-5\",\n",
    "    query_language=\"en\",\n",
    "    debug=QueryDebugMode.QUERY_REWRITES,\n",
    "    query_caption=QueryCaptionType.EXTRACTIVE,\n",
    "    query_answer=QueryAnswerType.EXTRACTIVE,\n",
    "    top=3\n",
    ")\n",
    "\n",
    "text_query_rewrites = results.get_debug_info().query_rewrites.text.rewrites\n",
    "vector_query_rewrites = results.get_debug_info().query_rewrites.vectors[0].rewrites\n",
    "print_results(results)\n",
    "\n",
    "print(\"Text Query Rewrites:\")\n",
    "print(text_query_rewrites)\n",
    "print(\"Vector Query Rewrites:\")\n",
    "print(vector_query_rewrites)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
